﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JSHGLASERComClassLibrary;
using System.Threading;
using System.Collections;
using System.Collections.Concurrent;
namespace ClientTest
{
   public class message
    {
        public List<byte> arry = new List<byte>();
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        MyClient myclient;//= new MyClient()
        private void Form1_Load(object sender, EventArgs e)
        {
            NetInterfaceinfo inf0 = new NetInterfaceinfo();
            inf0.CorS = netApplyInterfaceChose.Clience;
            inf0.Ip = "192.168.2.194";
            inf0.Port = 1500;
            inf0.OnClienceConnectServerEvent = ClienceConnectServerComneteventdelegateCC;
            inf0.OnGetBufBackFunction = ComnetDataReceiveddelegatecc;
            myclient = new MyClient(ref inf0);

            if (myclient.m_bConnect == false && myclient.Open())
            {
                Thread disthread = new Thread(back_recv_thread);
                disthread.Priority = ThreadPriority.Normal;
                disthread.IsBackground = true;
                disthread.Start();
            }
        }

        public void ClienceConnectServerComneteventdelegateCC(ref string cip, ref string str)
        {
            if(str == "ClientCannotConnecttoserver")
            {
               this.BeginInvoke(new EventHandler(delegate
              {
                    this.Close();
              }) );
         
            }
        }
          public  void ComnetDataReceiveddelegatecc(ref byte[] buf, uint length)
          {

            if (buf[1] == 0x00)
            {
                message mesdata = new message();
                int tta =(int) length;
                byte[] heat = buf.Skip(0).Take(tta).ToArray();
                mesdata.arry.AddRange(heat);
                mesqq.Enqueue(mesdata);
            }

        }
        private ConcurrentQueue<message> mesqq = new ConcurrentQueue<message>();       
        static int heatCC = 0;
        ushort qqde = 1;
        private  byte sumcheck(byte[] buf, int offest, int len)
        {
            byte b = 0;
            int num = 0;
            while (len-- > 0)
            {
                b = (byte)(b + buf[offest + num]);
                num++;
            }

            return b;
        }
        void back_recv_thread(object getobj)
        {

            while(true)
            {
                Thread.Sleep(10);
                heatCC++;
                if(heatCC*10 >1000 *1)
                {
                    heatCC = 0;
                    message mesheat = new message();
                    qqde++;
                    byte[] heat = new byte[10] { 0xf3, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xf5, 0xf4 };
                    heat[2]=( byte)( (qqde&0xff00) >>8);
                    heat[3] = (byte)((qqde & 0xff) >> 0);
                    heat[8] = sumcheck(heat, 0, 8);
                    mesheat.arry.AddRange(heat);
                    mesqq.Enqueue(mesheat);
                }

                while (mesqq.Count>0)
                {
                    if(mesqq.TryDequeue(out message mesa))
                    {
                     byte []sent =   mesa.arry.ToArray();

                        myclient.WriteData(ref sent);

                    }else
                    {

                    }
                }

            }

        }

    }
}
